home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / TestParts / Draw / PrtElmAc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  11.3 KB  |  424 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrtElmAc.cpp
  3.                 
  4.     Contains:    Implementation of class that handles accessing elements for
  5.                 the OpenDoc ContainerPart (formerly DrawPart)
  6.  
  7.     Owned by:    Bennet Marks
  8.  
  9.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  10.     
  11.     Based on:    PrtPrpAc.cpp (owned by Nick Pilch)
  12.  
  13.     Change History (most recent first):
  14.  
  15.          <9>     9/18/95    JP        1285318: Made it cope with ODGetComments
  16.                                     returning NULL
  17.          <8>     9/11/95    JP        1269048 & 1283405: Use ODGet/SetComments
  18.          <7>      9/4/95    TJ        Added Includes to Compile with out
  19.                                     PC-Headers.
  20.          <6>     8/25/95    JP        1262410: Comments part to frame fallout
  21.          <5>     8/11/95    jpa        Removed duplicate MyAECoerceDescPtr
  22.                                     [1276412]
  23.          <4>     6/26/95    JBS        &DMc&TÇ 1242642 BB: Refcounting Fixes.
  24.          <3>     6/22/95    NP        1261960: draw part code to extract
  25.                                     character for token accessed disposed
  26.                                     memory.
  27.          <2>     5/26/95    RR        #1251403: Multithreading naming support
  28.          <1>     4/26/95    BM        first checked in
  29.          <1>     04/13/95    BHM        File first created
  30.  
  31.     To Do:
  32. */
  33. #ifndef _ALTPOINT_
  34. #include "AltPoint.h"            // Use C++ savvy ODPoint and ODRect
  35. #endif
  36.  
  37. #ifndef _PRTELMAC_
  38. #include "PrtElmAc.h"
  39. #endif
  40.  
  41. #ifndef _DRWSHARED_
  42. #include "DrwShared.h"
  43. #endif
  44.  
  45. #ifndef SOM_ODDraft_xh
  46. #include <Draft.xh>
  47. #endif
  48.  
  49. #ifndef SOM_Module_AppleTestDraw_defined
  50. #include "DrawPart.xh"
  51. #endif
  52.  
  53. #ifndef SOM_ODFacet_xh
  54. #include "Facet.xh"
  55. #endif
  56.  
  57. #ifndef SOM_ODFrame_xh
  58. #include "Frame.xh"
  59. #endif
  60.  
  61. #ifndef SOM_ODPartWrapper_xh
  62. #include "PartWrap.xh"
  63. #endif
  64.  
  65. #ifndef SOM_ODFrameFacetIterator_xh
  66. #include "FrFaItr.xh"
  67. #endif
  68.  
  69. #ifndef _SEUTILS_
  70. #include "SEUtils.h"
  71. #endif
  72.  
  73. #ifndef SOM_ODTransform_xh
  74. #include "Trnsform.xh"
  75. #endif
  76.  
  77. #ifndef _EXCEPT_
  78. #include "Except.h"
  79. #endif
  80.  
  81. #ifndef _ORDCOLL_
  82. #include "OrdColl.h"
  83. #endif
  84.  
  85. #ifndef SOM_ODWindow_xh
  86. #include <Window.xh>
  87. #endif
  88.  
  89. #ifndef __AEREGISTRY__
  90. #include <AERegistry.h>
  91. #endif
  92.  
  93. #ifndef SOM_ODShape_xh
  94. #include <Shape.xh>
  95. #endif
  96.  
  97. #ifndef __TEXTEDIT__
  98. #include <TextEdit.h>
  99. #endif
  100.  
  101. #ifndef _ODDEBUG_
  102. #include "ODDebug.h"    // Adkins -- added
  103. #endif
  104.  
  105. #ifndef __AEOBJECTS__
  106. #include <AEObjects.h>
  107. #endif
  108.  
  109. #ifndef _INFOUTIL_
  110. #include <InfoUtil.h>
  111. #endif
  112.  
  113. #ifndef _TEMPOBJ_
  114. #include "TempObj.h"
  115. #endif
  116.  
  117. #ifndef _ITEXT_
  118. #include "IText.h"
  119. #endif
  120.  
  121. #pragma segment PrtPrpEl
  122.  
  123. //==============================================================================
  124. // Constants
  125. //==============================================================================
  126.  
  127. //==============================================================================
  128. // Scalar Types
  129. //==============================================================================
  130.  
  131. //==============================================================================
  132. // Local Classes
  133. //==============================================================================
  134.  
  135. //==============================================================================
  136. // Global Variables
  137. //==============================================================================
  138.  
  139. //==============================================================================
  140. // DrawPartElemAccessor
  141. //==============================================================================
  142.  
  143. //------------------------------------------------------------------------------
  144. // DrawPartElemAccessor::DrawPartElemAccessor
  145. //------------------------------------------------------------------------------
  146. DrawPartElemAccessor::DrawPartElemAccessor()
  147. {
  148.     fElemClass = typeNull;
  149.     fForm = typeNull;
  150.     fSelectionData = kODNULL;
  151.     fFrame = kODNULL;
  152.     MAKE_DESC_NULL(fSelDataDesc);
  153. }    //DrawPartElemAccessor
  154.  
  155. //------------------------------------------------------------------------------
  156. // DrawPartElemAccessor::InitElemAccessor
  157. //------------------------------------------------------------------------------
  158. void DrawPartElemAccessor::InitElemAccessor(DescType elemClass, DescType form, 
  159.             AEDesc* selectionData, ODFrame* frame)
  160. {
  161.     fElemClass = elemClass;
  162.     fForm = form;
  163.     fFrame = frame;
  164.     fSelectionData = &fSelDataDesc;
  165.     THROW_IF_ERROR( AEDuplicateDesc( selectionData, fSelectionData ) );
  166. }    //InitElemAccessor
  167.  
  168. //------------------------------------------------------------------------------
  169. // DrawPartElemAccessor::~DrawPartElemAccessor
  170. //------------------------------------------------------------------------------
  171. DrawPartElemAccessor::~DrawPartElemAccessor()
  172. {
  173.     if DESC_IS_NOT_NULL( *GetSelectionData() ) AEDisposeDesc(fSelectionData);
  174. }    //~DrawPartElemAccessor
  175.  
  176. //------------------------------------------------------------------------------
  177. // DrawPartElemAccessor::GetData
  178. //------------------------------------------------------------------------------
  179. void DrawPartElemAccessor::GetData(AEDesc* result)
  180. {
  181.     char    returnChar;
  182.     
  183.     MAKE_DESC_NULL(*result);
  184.     
  185.     returnChar = GetOneChar();
  186.     
  187.     THROW_IF_ERROR( AECreateDesc( typeChar, &returnChar, 1, result ));
  188. }    //GetData
  189.  
  190. //------------------------------------------------------------------------------
  191. // DrawPartElemAccessor::SetData
  192. //------------------------------------------------------------------------------
  193. void DrawPartElemAccessor::SetData(AEDesc* data)
  194. {
  195.     Environment*    ev = somGetGlobalEnvironment();
  196.     ODSLong            index;
  197.     Size            actualSize;
  198.     ODIText*        iText = kODNULL;
  199.     char*            charPtr;
  200.     char            dataChar;
  201.     ODError         err = noErr;
  202.         
  203.     TRY
  204.     
  205.         if ( GetElemClass() != cChar ) THROW( errAEUnknownObjectType );
  206.         if ( GetForm() != formAbsolutePosition ) THROW( errAEBadKeyForm );
  207.         
  208.         THROW_IF_ERROR( MyAECoerceDescPtr( *GetSelectionData(), typeLongInteger,
  209.             (Ptr)&index, sizeof(index), &actualSize ) );
  210.         
  211.         if ( index < 1 ) THROW( errAENoSuchObject );
  212.         
  213.         ODFrame* frame = GetFrame();
  214.         
  215.         iText = ODGetComments( ev, frame, kODNULL );
  216.     
  217.         if ( !iText || index > GetITextStringLength(iText) ) THROW( errAENoSuchObject );
  218.     
  219.         charPtr = GetITextPtr(iText) + index - 1;
  220.     
  221.         THROW_IF_ERROR( MyAECoerceDescPtr( *data, typeChar,
  222.             (Ptr)&dataChar, 1, &actualSize ) );
  223.             
  224.         if ( actualSize > 1 ) THROW(errAECoercionFail);
  225.         
  226.         *charPtr = dataChar;
  227.  
  228.         ODSetComments( ev, frame, iText );
  229.         
  230. #if ODDebug
  231.         GetFrame()->Invalidate(ev, (ODShape*)kODNULL, kODNULL);
  232. #endif
  233.         
  234.     CATCH_ALL
  235.         err = ErrorCode();
  236.     ENDTRY
  237.     
  238.     if (iText != kODNULL) DisposeIText(iText);
  239.     
  240.     THROW_IF_ERROR(err);
  241. }    //SetData
  242.  
  243. //------------------------------------------------------------------------------
  244. // DrawPartElemAccessor::GetOneChar
  245. //------------------------------------------------------------------------------
  246. char DrawPartElemAccessor::GetOneChar()
  247. {
  248.     Environment*    ev = somGetGlobalEnvironment();
  249.     ODSLong            index;
  250.     Size            actualSize;
  251.     ODIText*        iText = kODNULL;
  252.     char            theChar;
  253.     ODError         error = noErr;
  254.     
  255.     TRY
  256.         if ( GetElemClass() != cChar ) THROW( errAEUnknownObjectType );
  257.         if ( GetForm() != formAbsolutePosition ) THROW( errAEBadKeyForm );
  258.     
  259.         THROW_IF_ERROR( MyAECoerceDescPtr( *GetSelectionData(), typeLongInteger,
  260.             (Ptr)&index, sizeof(index), &actualSize ) );
  261.     
  262.         //NOTE: 1-based: first character has index = 1
  263.         
  264.         if ( index < 1 ) THROW( errAENoSuchObject );
  265.         
  266.         ODFrame* frame = GetFrame();
  267.         
  268.         iText = ODGetComments( ev, frame, kODNULL );
  269.     
  270.         if ( !iText || index > GetITextStringLength(iText) ) THROW( errAENoSuchObject );
  271.     
  272.         theChar = *(GetITextPtr(iText) + index - 1);
  273.         
  274.     CATCH_ALL
  275.         error = ErrorCode();
  276.     ENDTRY
  277.     
  278.     if (iText != kODNULL) DisposeIText(iText);
  279.     
  280.     THROW_IF_ERROR(error);
  281.  
  282.     return theChar;
  283. }    //GetOneChar
  284.  
  285. //------------------------------------------------------------------------------
  286. // DrawPartElemAccessor::GetFrame
  287. //------------------------------------------------------------------------------
  288. ODFrame* DrawPartElemAccessor::GetFrame()
  289. {
  290.     return fFrame;
  291. }    //GetFrame
  292.  
  293. //------------------------------------------------------------------------------
  294. // DrawPartElemAccessor::SetFrame
  295. //------------------------------------------------------------------------------
  296. void DrawPartElemAccessor::SetFrame(ODFrame* frame)
  297. {
  298.     fFrame = frame;
  299. }    //SetFrame
  300.  
  301.  
  302. //------------------------------------------------------------------------------
  303. // DrawPartElemAccessor::GetForm
  304. //------------------------------------------------------------------------------
  305. DescType DrawPartElemAccessor::GetForm()
  306. {
  307.     return fForm;
  308. }    //GetForm
  309.  
  310. //------------------------------------------------------------------------------
  311. // DrawPartElemAccessor::SetForm
  312. //------------------------------------------------------------------------------
  313. void DrawPartElemAccessor::SetForm(DescType form)
  314. {
  315.     fForm = form;
  316. }    //SetForm
  317.  
  318.  
  319. //------------------------------------------------------------------------------
  320. // DrawPartElemAccessor::GetSelectionData
  321. //------------------------------------------------------------------------------
  322. AEDesc* DrawPartElemAccessor::GetSelectionData()
  323. {
  324.     return fSelectionData;
  325. }    //GetSelectionData
  326.  
  327. //------------------------------------------------------------------------------
  328. // DrawPartElemAccessor::SetSelectionData
  329. //------------------------------------------------------------------------------
  330. void DrawPartElemAccessor::SetSelectionData(AEDesc* selectionData)
  331. {
  332.     fSelectionData = selectionData;
  333. }    //SetSelectionData
  334.  
  335. //------------------------------------------------------------------------------
  336. // DrawPartElemAccessor::GetElemClass
  337. //------------------------------------------------------------------------------
  338. DescType DrawPartElemAccessor::GetElemClass()
  339. {
  340.     return fElemClass;
  341. }    //GetElemClass
  342.  
  343. //------------------------------------------------------------------------------
  344. // DrawPartElemAccessor::SetElemClass
  345. //------------------------------------------------------------------------------
  346. void DrawPartElemAccessor::SetElemClass(DescType elemClass)
  347. {
  348.     fElemClass = elemClass;
  349. }    //SetElemClass
  350.  
  351. //==============================================================================
  352. // Utility Functions
  353. //==============================================================================
  354.  
  355. //------------------------------------------------------------------------------
  356. // MyAECoerceDescPtr
  357. //------------------------------------------------------------------------------
  358. #if 0 /*Already defined in SEUtils.cpp*/
  359. ODError MyAECoerceDescPtr(
  360.             AEDesc        theAEDesc,
  361.             DescType    toType,
  362.             Ptr            dataPtr,
  363.             Size        maximumSize,
  364.             Size*        actualSize)
  365. {
  366.     ODError    error = noErr;
  367.     AEDesc        newDesc;
  368.     AEDesc        resultDesc;
  369.     Size        transferSize;
  370.     
  371.     MAKE_DESC_NULL(resultDesc);
  372.     
  373.     TRY
  374.     {
  375.         if ((toType == theAEDesc.descriptorType) || (toType == typeWildCard))
  376.             newDesc = theAEDesc;    //no need to coerce
  377.         else
  378.         {//must coerce desc
  379.             THROW_IF_ERROR( AECoerceDesc(&theAEDesc,toType,&resultDesc) );
  380.             newDesc = resultDesc;
  381.         }
  382.     
  383.         //special case for NIL handle - **CHECK what we should really do
  384.         if (newDesc.dataHandle == kODNULL) actualSize = 0;
  385.         else
  386.         {//not a NIL handle
  387.             *actualSize = GetHandleSize(newDesc.dataHandle);
  388.             THROW_IF_ERROR(MemError());
  389.         
  390.             transferSize = *actualSize;
  391.             if (maximumSize < transferSize) transferSize = maximumSize;
  392.         
  393.             BlockMove(*(newDesc.dataHandle), dataPtr, transferSize);
  394.         };
  395.     }
  396.     
  397.     CATCH_ALL
  398.     {
  399.         error = ErrorCode();
  400.     }
  401.     ENDTRY
  402.     
  403.     if DESC_IS_NOT_NULL(resultDesc) AEDisposeDesc(&resultDesc);
  404.     
  405.     return error;
  406. }    //MyAECoerceDescPtr
  407. #endif
  408.  
  409. //------------------------------------------------------------------------------
  410. // GetOneCharFromDesc
  411. //------------------------------------------------------------------------------
  412. char GetOneCharFromDesc(AEDesc* charDesc)
  413. {
  414.     char    resultChar;
  415.     Size    actualSize;
  416.     
  417.     THROW_IF_ERROR( MyAECoerceDescPtr( *charDesc, typeChar, &resultChar,
  418.         1, &actualSize ) );
  419.         
  420.     if ( actualSize > 1 ) THROW(errAECoercionFail);
  421.     
  422.     return resultChar;
  423. }
  424.